using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

using ViewOnCore.Helper.WPF;

namespace UI.Control
{

    sealed public class AnimatedWrapPanel : WrapPanel
    {

        #region Variables

        private Size _extent = new Size(0, 0);
        private Size _viewport = new Size(0, 0);

        private TranslateTransform _trans = new TranslateTransform();

        #endregion

        #region Constructor

        public AnimatedWrapPanel()
        {
            this.RenderTransform = _trans;

            PanelLayoutAnimator.SetIsAnimationEnabled (this, true);

            this.Margin = new Thickness(10, 5, 10, 5);

            this.Loaded += new RoutedEventHandler(AnimatedWrapPanel_Loaded);
        }

        void AnimatedWrapPanel_Loaded(object sender, RoutedEventArgs e)
        {
            ScrollContentPresenter scp = Infragistics.Windows.Utilities.GetAncestorFromType(this, typeof(ScrollContentPresenter), false) as ScrollContentPresenter;
            scp.SizeChanged += new SizeChangedEventHandler(scp_SizeChanged);
        }

        #endregion

        #region MeasureOverride

        void scp_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            ((ScrollContentPresenter)sender).InvalidateMeasure();
            ((ScrollContentPresenter)sender).InvalidateArrange();
            ((ScrollContentPresenter)sender).ScrollOwner.InvalidateScrollInfo();
            ((ScrollContentPresenter)sender).UpdateLayout();
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            ScrollContentPresenter scp = Infragistics.Windows.Utilities.GetAncestorFromType(this, typeof(ScrollContentPresenter), false) as ScrollContentPresenter;
            this.Width = scp.ActualWidth;
            return base.ArrangeOverride(new Size(scp.ActualWidth, 5000));
        }

        #endregion

    }

}
